//
//  VWPlayerErrorStateLayer.swift
//  VideoPlayer_Example
//
//  Created by JY_NEW on 2021/10/14.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import ijkPlayerManager
import UIKit
import UICreate

/// 错误状态
class VWPlayerErrorStateLayer: VWPlayerBaseLayer {
    
    override var category: VideoPlayerLayerCategory {
        return .state
    }
    
    override func loadPlayerManager(_ player: VideoPlayerManagerType) {
        player.observer.status.addTarget( self, action: #selector(self.onStatusChanged))
    }
    override func unloadPlayerManager(_ player: VideoPlayerManagerType) {
        player.observer.status.removeTarget(self)
    }
    
    override func loadContainerView(_ containerView: UIView) {
        containerView.backgroundColor = .black
        containerView.add(self.contentView, layout: .center)
    }
    
    @objc func onStatusChanged() {
        if self.playerManager?.status.isError ?? false {
            self.active()
        }else{
            self.deactive()
        }
    }
    
    override func show(completion: @escaping () -> Void) {
        self.aniamteShow(for: self.contentView, completion: completion)
    }
    override func hide(completion: @escaping () -> Void) {
        self.aniamteHide(for: self.contentView, completion: completion)
    }
    
    // MARK: 界面控件
    
    lazy var contentView: UIView = {
        let contentView = UIView()
        
        let image = UIImageView(.image("vw_play_load_error"))
        let title = UILabel(.text("出错了，请重试"), .font(.pingfang(medium: 13)), .color(.white))
        let button = UIButton(.text("重试"), .bg(.white), .size(68, 28), .corner(14), .font(.pingfang(12)), .color(.black), .event(self, #selector(self.onRetry)))
        contentView.add {
            image.layout(.top, .centerX)
            title.layout(.bottomTop(13), .for(.parent, .marginX))
            button.layout(.bottomTop(18), .centerX)
                .layout(for: .parent, .bottom)
        }
        
        return contentView
    }()
    @objc func onRetry() {
        self.deactive()
        self.playerManager?.play()
    }
}
